package com.sgb.kjwl.view.ui.fragment.machine;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentAskLeaseHallBinding;
import com.sgb.kjwl.di.machine.DaggerMachineFragComponent;
import com.sgb.kjwl.di.machine.MachineFragModule;
import com.sgb.kjwl.model.entity.lease.AskLeaseItemEntity;
import com.sgb.kjwl.viewmodel.machine.AskLeaseFragmentHallModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.FragPagerAdapter;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

public class AskLeaseHallFragment extends AppLazyFragment {

    @Inject
    AskLeaseFragmentHallModel viewModel;
    private FragmentAskLeaseHallBinding binding;
    private FragPagerAdapter pagerAdapter;
    private ArrayList<Fragment> mFragments;
    private String[] titles = {"全部", "机械求租", "设备求租", "车辆求租", "房产求租", "土地求租", "工厂求租", "农机求租"};
    private String[] titles2 = {"全部", "机械租赁", "设备租赁",};
    private int askPosition = 0;
    private int categoryType = 0;
    private boolean isFilter = true;

    public static AskLeaseHallFragment newInstance(int position, boolean isFilter) {
        AskLeaseHallFragment fragment = new AskLeaseHallFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("askPosition", position);
        bundle.putBoolean("isFilter", isFilter);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerMachineFragComponent.builder().appComponent(appComponent).machineFragModule(new MachineFragModule(this)).build().inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_ask_lease_hall, container, false);
        return binding.getRoot();
    }


    @Override
    protected void initView() {
        if (getArguments() != null) {
            askPosition = getArguments().getInt("askPosition", 0);
            isFilter = getArguments().getBoolean("isFilter", true);
            categoryType = getArguments().getInt("categoryType", 0);
        }
    }

    @Override
    protected void onLazyLoad() {
        viewModel.getTypeModel();
        mFragments = new ArrayList<>();
    }

    public void showTypeView(List<AskLeaseItemEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity>>>>> datas) {
        if (datas == null || datas.size() == 0) {
            return;
        }
        mFragments = new ArrayList<>();
        if (askPosition == 0) {
            mFragments.add(AskLeaseFragment.newInstance("", new ArrayList<>(),isFilter));
            for (int i = 0; i < datas.size(); i++) {
                ArrayList<AskLeaseItemEntity<List<AskLeaseItemEntity>>> arrayList = new ArrayList<>();
                arrayList.addAll(datas.get(i).getChildren());
                mFragments.add(AskLeaseFragment.newInstance(datas.get(i).getType(), arrayList,isFilter));
            }
            pagerAdapter = new FragPagerAdapter(getChildFragmentManager(), mFragments);
            binding.viewpager.setAdapter(pagerAdapter);
            binding.viewpager.setOffscreenPageLimit(titles.length);
            binding.tablayout.setViewPager(binding.viewpager, titles);
            binding.viewpager.setCurrentItem(categoryType);
        } else if (askPosition == 1) {
            mFragments.add(AskLeaseBiddingFragment.newInstance("", new ArrayList<>(),isFilter));
            mFragments.add(AskLeaseBiddingFragment.newInstance("0", (ArrayList<AskLeaseItemEntity<List<AskLeaseItemEntity>>>) datas.get(0).getChildren(),isFilter));
            mFragments.add(AskLeaseBiddingFragment.newInstance("1", (ArrayList<AskLeaseItemEntity<List<AskLeaseItemEntity>>>) datas.get(1).getChildren(),isFilter));
            pagerAdapter = new FragPagerAdapter(getChildFragmentManager(), mFragments);
            binding.viewpager.setAdapter(pagerAdapter);
            binding.viewpager.setOffscreenPageLimit(titles2.length);
            binding.tablayout.setViewPager(binding.viewpager, titles2);
            binding.viewpager.setCurrentItem(categoryType);
        } else if (askPosition == 2) {
            mFragments.add(AskLeasePickFragment.newInstance("", new ArrayList<>(),isFilter));
            mFragments.add(AskLeasePickFragment.newInstance("0", (ArrayList<AskLeaseItemEntity<List<AskLeaseItemEntity>>>) datas.get(0).getChildren(),isFilter));
            mFragments.add(AskLeasePickFragment.newInstance("1", (ArrayList<AskLeaseItemEntity<List<AskLeaseItemEntity>>>) datas.get(1).getChildren(),isFilter));
            pagerAdapter = new FragPagerAdapter(getChildFragmentManager(), mFragments);
            binding.viewpager.setAdapter(pagerAdapter);
            binding.viewpager.setOffscreenPageLimit(titles2.length);
            binding.tablayout.setViewPager(binding.viewpager, titles2);
            binding.viewpager.setCurrentItem(categoryType);
        }
    }

    public void changeType(int position) {
        binding.viewpager.setCurrentItem(position);
    }

}

